/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.xtd.facade.employment;

import com.xtd.facade.AbstractFacade;
import com.xtd.model.employment.DepartmentPost;
import com.xtd.model.employment.Employee;
import java.util.List;
import javax.ejb.Stateless;

/**
 *
 * @author Lu Yuanbin
 */
@Stateless
public class EmployeeFacade extends AbstractFacade<Employee> {

    public EmployeeFacade() {
        super(Employee.class);
    }

    public List<Employee> searchByContent(String content) {
        if (content == null) {
            content = "";
        }
        return getEntityManager().createQuery("select o from Employee as o where o.name like :content or o.phone like :content").setParameter("content", "%" + content + "%").getResultList();
    }

    public List<Employee> searchByDepartment(String department) {
        return getEntityManager().createQuery("select o from Employee as o where o.departmentPost.name = :department").setParameter("department", department).getResultList();
    }

    public List<Employee> searchByDepartmentPost(String department, String post) {
        return getEntityManager().createQuery("select o from Employee as o where o.departmentPost.name = :department and o.departmentPost.post = :post").setParameter("department", department).setParameter("post", post).getResultList();
    }

    public void update(Employee selectedEmployee) {
        if (selectedEmployee.isNew()) {
            create(selectedEmployee);
        } else {
            edit(selectedEmployee);
        }
    }

    public List<Employee> findAllActive() {
        return getEntityManager().createQuery("select o from Employee as o where o.active is true").getResultList();
    }

    public Employee findByUser(String username) {
        return (Employee) getEntityManager().createQuery("select o from Employee as o where o.xtduser.username = :username").setParameter("username", username).getSingleResult();
    }

    public List<Employee> searchByDepartment(DepartmentPost departmentPost) {
        return getEntityManager().createQuery("select o from Employee as o where o.departmentPost = :departmentPost").setParameter("departmentPost", departmentPost).getResultList();
    }
}
